package com.zyc.auto;

import com.zyc.pojo.Table;
import com.zyc.util.HappyUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * 作者：Rem
 * 时间：2021/1/28 13:57
 */
public class AutoController {
    /**
     * 目标文件夹
     */
    private final String pathStr;

    /**
     * 包名前缀
     */
    private final String namespace;

    /**
     * 创建类时常量赋值
     */
    public AutoController() {
        pathStr = HappyUtil.properties.getProperty("targetPath");
        namespace = HappyUtil.properties.getProperty("namespace");
    }

    /**
     * 方法入口
     */
    public void start() throws IOException {
        autoResult();
        List<Table> tableList = HappyUtil.getTableList();
        for (Table table : tableList) {
            auto(table);
        }
    }

    /**
     * 生成指定表的Controller层
     */
    private void auto(Table table) throws IOException {
        String className = table.getTableUpperCamelName();
        String pojoName = table.getTableLowerCamelName();
        //写文件到硬盘
        File path = new File(pathStr + "/controller");
        if (!path.exists() && !path.mkdirs()) {
            System.err.println("文件夹生成错误！" + path);
            return;
        }
        File file = new File(pathStr + "/controller/" + className + "Controller.java");
        System.out.println(file.getAbsolutePath());
        FileWriter fw = new FileWriter(file);
        String controllerText = "package " + namespace + ".controller;\n" +
                "\n" +
                "import " + namespace + ".service." + className + "Service;\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import javax.annotation.Resource;\n" +
                "import io.swagger.annotations.Api;\n" +
                "import org.springframework.web.bind.annotation.RestController;\n\n" +
                //拼接注解
                "@Slf4j\n" +
                "@RestController\n" +
                "@RequestMapping(\"/" + pojoName + ".do\")\n" +
                "@Api(value = \"" + pojoName + "模块\", tags = {\"" + pojoName + "模块\"})" +
                "public class " + className + "Controller {\n" +
                "    @Resource\n" +
                "    private " + className + "Service " + pojoName + "Service;\n\n" +
                "}";
        fw.write(controllerText);
        HappyUtil.closeAll(fw);
    }

    /**
     * 在pojo生成ResultDate
     */
    private void autoResult() throws IOException {
        String resultDataText = "package " + namespace + ".pojo;\n" +
                "import io.swagger.annotations.ApiModel;\n" +
                "import io.swagger.annotations.ApiModelProperty;\n" +
                "import lombok.AllArgsConstructor;\n" +
                "import lombok.Data;\n" +
                "import lombok.NoArgsConstructor;\n" +
                "import org.jetbrains.annotations.NotNull;\n" +
                "@Data\n" +
                "@AllArgsConstructor\n" +
                "@NoArgsConstructor\n" +
                "@ApiModel(\"返回的结构对象ResultData\")\n" +
                "public class ResultData<T>{\n" +
                "    @ApiModelProperty(\"查询响应码，0表示成功\")\n" +
                "    private Integer code;\n" +
                "    @ApiModelProperty(\"响应信息\")\n" +
                "    private String msg;\n" +
                "    @ApiModelProperty(\"响应数据\")\n" +
                "    private T data;\n" +
                "    public ResultData(Integer code, String msg) {\n" +
                "        this.code = code;\n" +
                "        this.msg = msg;\n" +
                "    }\n" +
                "    @NotNull\n" +
                "    public static ResultData<?> Success(String msg) {\n" +
                "        return new ResultData<>(Constants.SUCCESS, msg);\n" +
                "    }\n" +
                "\n" +
                "    @NotNull\n" +
                "    public static <T1> ResultData<T1> Success(String msg, T1 obj) {\n" +
                "        return new ResultData<>(Constants.SUCCESS, msg, obj);\n" +
                "    }" +
                "}\n";
        File path = new File(pathStr + "/pojo");
        if (!path.exists() && !path.mkdirs()) {
            System.err.println("文件夹生成错误！" + path);
            return;
        }
        File file = new File(pathStr + "/pojo/ResultData.java");
        System.out.println(file.getAbsolutePath());
        FileWriter fw = new FileWriter(file);
        fw.write(resultDataText);
        HappyUtil.closeAll(fw);
    }
}
